#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
#include <string.h>

typedef struct Student {
    int id;
    char name[5];
    struct Student* next;
} Student;

int student_count = 0;

Student* create_student_list() {
    Student* head = (Student*)malloc(sizeof(Student));
    head->id = 0;
    head->next = NULL;
    return head;
}

Student* add_student(Student* head) {
    Student* backup = head;
    while (head->next != NULL) {
        head = head->next;
    }
    int id = 0;
    char name[5];
    printf("Input an id:");
    scanf("%d", &id);
    printf("Input a name(max:4):");
    scanf("%s", name);
    Student* node = (Student*)malloc(sizeof(Student));
    node->id = id;
    strcpy(node->name, name);
    node->next = NULL;
    head->next = node;
    head = head->next;
    student_count++;
    return backup;
}

bool edit_student(Student* head, int edit_id) {
    Student* backup = head;
    while (head->id != edit_id && head->next != NULL) {
        head = head->next;
    }
    char name[5];
    if (head->id == edit_id) {
        printf("Input a name(max:4):");
        scanf("%s", name);
        strcpy(head->name, name);
        head = backup;
        return true;
    }
    else {
        head = head->next;
        if (head->id == edit_id) {
            strcpy(head->name, name);
            head = backup;
            return true;
        }
        else {
            head = backup;
            return false;
        }
    }
}

void delete_student(Student* head) {
    printf("Input delete id:");
    int id;
    scanf("%d", &id);
    Student* backup = head;
    Student* prev = head;
    while (head->next != NULL && head->id != id) {
        prev = head;
        head = head->next;
    }
    if (head->id == id) {
        Student* tmp = head->next;
        free(head);
        prev->next = tmp;
        student_count--;
    }
    head = backup;
}

void delete_all_student(Student* head) {
    Student* backup = head;
    head = head->next;
    while (head != NULL) {
        Student* next = head->next;
        free(head);
        head = next;
    }
    head = backup;
    head->next = NULL;
    student_count = 0;
}

bool save_to_file(Student* head) {
    if (student_count == 0) {
        return true;
    }
    FILE* file = fopen("db.txt", "w");
    if (file == NULL) {
        return false;
    }
    int str_length = 0;
    char* content = (char*)malloc(sizeof(char) * (student_count * 17) + 1);
    content = memset(content, 0, str_length * 17 + 1);
    char buffer[17] = {0};
    Student* backup = head;
    for (int i = 0; i < student_count; ++i) {
        head = head->next;
        if (head == NULL) {
            break;
        }
        sprintf(buffer, "id=%d,name=%s;", head->id, head->name);
        content = strcat(content, buffer);
    }
    fwrite(content, student_count * 16, 1, file);
    head = backup;
    fclose(file);
    return true;
}

Student* search_student(Student* head) {
    Student* backup = head;
    Student* ret = NULL;
    int id;
    printf("Input search id:");
    scanf("%d", &id);
    while (head->id != id && head->next != NULL) {
        head = head->next;
    }
    if (head->id == id) {
        ret = head;
        head = backup;
        return ret;
    }
    else if (head->next != NULL) {
        head = head->next;
        if (head->id == id) {
            ret = head;
            return ret;
        }
        else {
            head = backup;
            return ret;
        }
    }
    else {
        return ret;
    }
}

int main() {
    Student* head = create_student_list();
    Student* backup = NULL;
    bool is_edit;
    Student* node = NULL;
    int search_id;
    for (;;) {
        printf("1. Add student\n");
        printf("2. Edit student\n");
        printf("3. Delete one student\n");
        printf("4. Delete all students\n");
        printf("5. Search student\n");
        printf("6. Save to file\n");
        printf("Please input a option number:");
        int option = 0;
        scanf("%d", &option);
        switch (option) {
        case 1:
            backup = add_student(head);
            break;
        case 2:
            scanf("%d", &search_id);
            is_edit = edit_student(head, search_id);
            if (is_edit) {
                printf("Success\n");
            }
            else {
                printf("Failed\n");
            }
            break;
        case 3:
            delete_student(head);
            break;
        case 4:
            delete_all_student(head);
            break;
        case 5:
            node = search_student(head);
            if (node == NULL) {
                printf("404\n");
            }
            else {
                printf("id: %d\tname: %s\n", node->id, node->name);
            }
            break;
        case 6:
            save_to_file(head);
            break;
        default:
            printf("None of a number for this option.\n");
            printf("Please input another one:");
            break;
        }
    }

    return 0;
}
